<template>
  <!--优秀案例提报新增  -->
  <div>
    <div>
      <FormPanel
        ref="formPanel"
        :list="initForm"
        :cols="2"
        :formType="type"
        :labelWidth="130"
        :isSubmitBtn="false"
        @formChange="formChangeHandle"
      />
    </div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDialog">关 闭</el-button>
      <el-button :loading="saveLoading" @click="saveDialog" type="primary">保 存</el-button>
      <el-button :loading="publishLoading" @click="publishDialog" type="primary">上 报</el-button>
    </div>

    <!-- 案件征集弹窗 -->
    <BaseDialog
      title="案件征集查询"
      :visible.sync="visibleCaseCollect"
      width="1000px"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <caseCollectMadal
        :fatherReq="reqRepairDealer"
        :fatherGoBack="goBackRepairDealer"
        ref="caseCollectDialog"
        @close="closecaseCollect"
      />
    </BaseDialog>

    <!-- 项目计划弹窗 -->
    <BaseDialog
      title="项目计划查询"
      :visible.sync="visibleProjectPlan"
      width="1000px"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <projectPlanMadal
        :fatherReq="reqRepairDealer1"
        :fatherGoBack="goBackRepairDealer1"
        :initDate="initDate"
        ref="projectPlanDialog"
        @close="closeprojectPlan"
      />
    </BaseDialog>
    <!-- 项目详情弹窗 -->
    <BaseDialog
      title="项目详情查询"
      :visible.sync="visibleProjectPlanDetail"
      width="1000px"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <info v-model="initData" ref="projectPlanDetailDialog" @close="closeprojectPlanDetail" />
    </BaseDialog>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { addOutStandingCase, getPlanById } from '@/api/cms-api/projectOperating/case/oemCollect/index';
import caseCollectMadal from './caseCollectMadal';
import projectPlanMadal from './projectPlanMadal';
import info from '../../summary/oemInquire/info';
// import { getBigMarketType, getSmallMarketType, getSummaryOemInquireList, selectListByBill } from '@/api/projectOperating/plan/index';
export default {
  mixins: [dictionary],
  components: {
    caseCollectMadal,
    projectPlanMadal,
    info
  },
  props: {
    type: {
      type: String,
      required: true
    },
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      initForm: this.initFormFun(),
      formValue: {},
      visibleCaseCollect: false,
      visibleProjectPlan: false,
      visibleProjectPlanDetail: false,
      caseLevyId: '', //案例征集ID
      caseUrlList: [],
      saveLoading: false,
      publishLoading: false,
      initData: {},
      planRow: {}, //初始化项目字段详情
      planDetailLoading: false,
      initDate: {}
    };
  },
  mounted() {
    // console.log(this.value); //获取到行id 、类型（新增、详情）
    //请求接口进行赋值，自己写吧
  },
  methods: {
    reqRepairDealer(val) {
      this.initForm.find(item => item.fieldName === 'caseLevyCode').initialValue = val.caseLevyCode;
      this.initForm.find(item => item.fieldName === 'caseLevyName').initialValue = val.caseLevyName;
      this.initForm.find(item => item.fieldName === 'levyStartDate').initialValue = val.levyStartDate;
      this.initForm.find(item => item.fieldName === 'levyEndDate').initialValue = val.levyEndDate;
      this.initForm.find(item => item.fieldName === 'projectExecuteStartDate').initialValue = val.projectExecuteStartDate;
      this.initForm.find(item => item.fieldName === 'projectExecuteEndDate').initialValue = val.projectExecuteEndDate;
      this.initForm.find(item => item.fieldName === 'caseLevyClaim').initialValue = val.caseLevyClaim;
      this.initForm.find(item => item.fieldName === 'collectFile').initialValue = val.collectFile;
      this.planStartDateBegin = val.projectExecuteStartDate;
      this.planStartDateEnd = val.projectExecuteEndDate;
      this.caseLevyId = val.caseLevyId;
      this.visibleCaseCollect = false;
    },
    reqRepairDealer1(val) {
      this.initForm.find(item => item.fieldName === 'planCode').initialValue = val.planCode;
      this.initForm.find(item => item.fieldName === 'projectName').initialValue = val.projectName;
      this.initForm.find(item => item.fieldName === 'projectFile').initialValue = val.projectFile;
      this.planId = val.planId;
      // this.businessId = val.orgId;
      this.ownerCode = val.ownerCode;
      this.visibleProjectPlan = false;
    },
    //关闭案例征集弹框
    goBackRepairDealer() {
      this.visibleCaseCollect = false;
    },
    //关闭项目计划弹窗
    goBackRepairDealer1() {
      this.visibleProjectPlan = false;
    },
    //搜索数据
    initFormFun() {
      return [
        {
          type: 'INPUT',
          label: '优秀案例编号',
          fieldName: 'caseCode',
          initialValue: '系统自动生成',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '案例征集编号',
          fieldName: 'caseLevyCode',
          placeholder: '请选择',
          initialValue: '',
          readonly: true,
          rules: [{ required: true, message: '请选择案例征集编号', trigger: 'blur' }],
          unitRender: () => {
            return <el-button slot="append" icon="el-icon-search" onClick={() => this.openCaseCollectDialog()}></el-button>;
          }
        },
        {
          type: 'INPUT',
          label: '案例征集名称',
          fieldName: 'caseLevyName',
          initialValue: '',
          disabled: true,
          placeholder: ''
        },
        {
          type: 'DATE',
          label: '征集开始日期',
          fieldName: 'levyStartDate',
          initialValue: '',
          disabled: true,
          placeholder: ''
        },
        {
          type: 'DATE',
          label: '征集结束日期',
          fieldName: 'levyEndDate',
          initialValue: '',
          disabled: true,
          placeholder: ''
        },
        {
          type: 'DATE',
          label: '项目执行开始日期',
          fieldName: 'projectExecuteStartDate',
          initialValue: '',
          disabled: true,
          placeholder: ''
        },
        {
          type: 'DATE',
          label: '项目执行结束日期',
          fieldName: 'projectExecuteEndDate',
          initialValue: '',
          disabled: true,
          placeholder: ''
        },
        {
          type: 'TEXT_AREA',
          label: '方案征集要求及说明',
          fieldName: 'caseLevyClaim',
          initialValue: '',
          rows: 4,
          selfCols: 2,
          disabled: true,
          placeholder: ''
        },
        {
          type: 'UPLOAD_FILE',
          label: '征集附件',
          disabled: true,
          fieldName: 'collectFile',
          initialValue: '',
          upload: {
            actionUrl: '/api/file/oss/upload'
          }
        },
        {
          type: 'INPUT',
          label: '项目编号',
          fieldName: 'planCode',
          placeholder: '请选择',
          initialValue: '',
          rules: [{ required: true, message: '请选择项目编号', trigger: 'blur' }],
          unitRender: () => {
            return (
              <div>
                <el-button slot="append" icon="el-icon-search" onClick={() => this.openProjectPlanDialog()}>
                  查询
                </el-button>
                <el-divider direction="vertical" />
                <el-button slot="append" icon="el-icon-document" onClick={() => this.planDetailDialog()}>
                  详情
                </el-button>
              </div>
            );
            // return <el-button slot="append" icon="el-icon-search" onClick={() => this.openProjectPlanDialog()}></el-button>;
          }
        },
        // {
        //   type: 'INPUT',
        //   label: '项目详情',
        //   unitRender: () => {
        //     return (
        //       <el-button type="primary" v-loading="planDetailLoading" slot="append" onClick={() => this.planDetailDialog()}>
        //         项目详情
        //       </el-button>
        //     );
        //   }
        // },
        {
          type: 'INPUT',
          label: '项目名称',
          fieldName: 'projectName',
          initialValue: '',
          rows: 4,
          readonly: true,
          placeholder: ''
        },
        {
          type: 'UPLOAD_FILE',
          label: '项目附件',
          fieldName: 'projectFile',
          initialValue: '',
          upload: {
            actionUrl: '/api/file/oss/upload',
            fileTypes: ['pdf', 'jpg', 'png', 'rar'],
            limit: 5,
            max: 1024 * 100
          }
        },
        {
          type: 'TEXT_AREA',
          label: '备注',
          fieldName: 'remark',
          initialValue: '',
          rows: 4,
          selfCols: 2,
          maxlength: 1000
        }
      ];
    },
    formChangeHandle(val) {
      this.formValue = val;
    },
    //新增保存
    async saveDialog() {
      this.$refs.formPanel.SUBMIT_FORM();
      if (null == this.$refs.formPanel.SUBMIT_FORM()) {
        return;
      }
      if (this.formValue !== null) {
        this.saveLoading = true;
        //追加隐藏状态字段
        this.formValue = Object.assign({}, { caseStatus: 25351001 }, { caseLevyId: this.caseLevyId }, { planId: this.planId }, this.formValue);
        let res = {};
        if (this.type === 'add') {
          if (this.formValue.projectFile != null) {
            for (let i = 0; i < this.formValue.projectFile.length; i++) {
              this.caseUrlList.push(Object.assign({}, { name: this.formValue.projectFile[i].name }, { url: this.formValue.projectFile[i].url }));
            }
            this.formValue = Object.assign({}, { fileUrl: this.caseUrlList }, this.formValue);
          }
          // this.formValue = Object.assign({}, { businessId: this.businessId }, this.formValue);
          this.formValue = Object.assign({}, { ownerCode: this.ownerCode }, this.formValue);
          // console.log(this.formValue);
          res = await addOutStandingCase(this.formValue);
          if (res.resultCode === 200) {
            this.saveLoading = false;
            this.$notify.success({ title: '提示信息', message: '新增成功!' });
            this.closeDialog('refresh');
          } else {
            this.saveLoading = false;
            this.$notify.warning({ title: '提示信息', message: res.errMsg });
          }
        }
      }
    },
    //上报
    async publishDialog() {
      this.$refs.formPanel.SUBMIT_FORM();
      if (null == this.$refs.formPanel.SUBMIT_FORM()) {
        return;
      }
      if (this.formValue !== null) {
        this.publishLoading = true;
        //追加隐藏状态字段
        this.formValue = Object.assign({}, { caseStatus: 25351002 }, { caseLevyId: this.caseLevyId }, { planId: this.planId }, this.formValue);
        let res = {};
        if (this.type === 'add') {
          if (this.formValue.projectFile != null) {
            for (let i = 0; i < this.formValue.projectFile.length; i++) {
              this.caseUrlList.push(Object.assign({}, { name: this.formValue.projectFile[i].name }, { url: this.formValue.projectFile[i].url }));
            }
            this.formValue = Object.assign({}, { fileUrl: this.caseUrlList }, this.formValue);
          }
          // this.formValue = Object.assign({}, { businessId: this.businessId }, this.formValue);
          this.formValue = Object.assign({}, { ownerCode: this.ownerCode }, this.formValue);
          res = await addOutStandingCase(this.formValue);
          if (res.resultCode === 200) {
            this.publishLoading = false;
            this.$notify.success({ title: '提示信息', message: '上报成功!' });
            this.closeDialog('refresh');
          } else {
            this.publishLoading = false;
            this.$notify.warning({ title: '提示信息', message: res.errMsg });
          }
        }
      }
    },
    closeDialog(param) {
      this.$emit('close', false, param);
    },
    //打开案例征集弹窗
    openCaseCollectDialog() {
      this.visibleCaseCollect = true;
    },
    //打开项目计划弹窗
    openProjectPlanDialog() {
      this.initDate = { initDateStart: this.planStartDateBegin, initDateEnd: this.planStartDateEnd };
      this.visibleProjectPlan = true;
    },
    //打开项目详情弹窗
    async planDetailDialog() {
      this.planDetailLoading = true;
      // let param = { planId: this.planId };
      if (this.planId != null) {
        let res = await getPlanById(this.planId);
        if (res.resultCode === 200) {
          this.planDetailLoading = false;
          this.planRow = res.data;
        }
        this.initData = {
          row: this.planRow,
          type: 'show' //add/edit/show
        };
        this.visibleProjectPlanDetail = true;
      } else {
        this.$notify.warning({ title: '提示信息', message: '请先查询选择项目！' });
      }
    },
    closecaseCollect(data) {
      this.visibleCaseCollect = data.visible;
      this.initForm[1].initialValue = '';
    },
    closeprojectPlan(data) {
      this.visibleProjectPlan = data.visible;
      this.initForm[8].initialValue = '';
    },
    closeprojectPlanDetail(data) {
      this.visibleProjectPlanDetail = false;
    }
  }
};
</script>
